﻿using System;
using System.Reflection;

namespace CodeSmithWpf;


public class PropertyDTO
{

    /// <summary>
    /// 属性注释
    /// </summary>
    public string PropertyDescription { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public PropertyInfo Property { get; set; }


    protected string GetPropertyTypeName(Type type)
    {
        if (type.IsGenericType)
        {
            if (type.Name == "List`1")
            {
                return "List<" + GetPropertyTypeName(type.GenericTypeArguments[0]) + ">";
            }
        }
        if (type.IsNullableType())
        {
            return type.GetNulllableType().Name + "?";
        }
        return type.Name;
    }

    public string GetPropertyName()
    {
        return Property.Name;
    }

    public string GetPropertyTypeName()
    {
        return GetPropertyTypeName(Property.PropertyType);
    }
    public string GetTypeScriptName()
    {
        //   return StringUtil.GetUnderlineCase(this.Property.Name);
      
        return System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(this.Property.Name);
    }

    public string GetTypeScriptTypeName()
    {
        if (Property.PropertyType.IsGenericType)
        {
            if (Property.PropertyType.Name == "List`1")
            {
                return "array<" + Property.PropertyType.GenericTypeArguments[0].GetTypeScriptTypeName() +">";
            }
        }
        if (Property.PropertyType.IsNullableType())
        {
            return Property.PropertyType.GetNulllableType().GetTypeScriptTypeName() + " | null";
        }
        return Property.PropertyType.GetTypeScriptTypeName();
    }

    public string GetTypeScriptFileName()
    {
        return StringUtil.GetUnderlineCase(this.Property.Name);
    }

    public string GetTypeScriptTypeValue()
    {
        if (Property.PropertyType.IsNullableType())
        {
            return "null";
        }
        string value= Property.PropertyType.GetTypeScriptTypeName();
        if (value == "boolean")
        {
            return "false";
        }else if (value == "number")
        {
            return "0";
        }
        return "''";
    }
}


